import { createVNode, render } from 'vue'
import XtxConfirm from './xtx-confirm.vue'

const div = document.createElement('div')
div.setAttribute('class', 'xtx-confirm-container')
document.body.appendChild(div)
// 该函数渲染XtxConfirm组件 标题和文本
// 函数的返回值是promise值
export default ({ text, title }) => {
  return new Promise((resolve, reject) => {
    // 点击确认
    const submitCallback = () => {
      render(null, div)
      resolve()
    }
    // 点击取消
    const cancelCallback = () => {
      render(null, div)
      reject(new Error('取消成功'))
    }

    const vnode = createVNode(XtxConfirm, { text, title, submitCallback, cancelCallback })
    render(vnode, div)
  })
}
